package com.charity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charity.domain.Project;
import com.charity.domain.Volunteer;
import com.charity.mapper.ProjectMapper;
import com.charity.mapper.VolunteerMapper;
import com.charity.service.IVolunteerService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

@Service
@Primary
public class VolunteerServiceImpl implements IVolunteerService {

    @Resource
    private VolunteerMapper volunteerMapper;
    
    @Resource
    private ProjectMapper projectMapper;

    @Override
    @Transactional
    public boolean register(Volunteer volunteer) {
        try {
            // 设置初始状态为待审核(0)
            volunteer.setStatus(0);
            return volunteerMapper.insert(volunteer) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean auditVolunteer(Long volunteerId, Integer status, String remark) {
        try {
            Volunteer volunteer = volunteerMapper.selectById(volunteerId);
            if (volunteer == null) {
                return false;
            }
            volunteer.setStatus(status);
            volunteer.setRemark(remark);
            return volunteerMapper.updateById(volunteer) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean createVolunteer(Volunteer volunteer) {
        try {
            return volunteerMapper.insert(volunteer) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateVolunteer(Volunteer volunteer) {
        try {
            return volunteerMapper.updateById(volunteer) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteVolunteer(Long volunteerId) {
        try {
            return volunteerMapper.deleteById(volunteerId) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Volunteer getVolunteerById(Long volunteerId) {
        try {
            return volunteerMapper.selectById(volunteerId);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Page<Volunteer> getVolunteerList(Integer status, int pageNum, int pageSize) {
        try {
            Page<Volunteer> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Volunteer> queryWrapper = new QueryWrapper<>();
            if (status != null) {
                queryWrapper.eq("status", status);
            }
            return volunteerMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            return new Page<>();
        }
    }

    @Override
    @Transactional
    public boolean joinProject(Long volunteerId, Long projectId) {
        try {
            // 检查志愿者是否存在且状态为已审核通过
            Volunteer volunteer = volunteerMapper.selectById(volunteerId);
            if (volunteer == null || volunteer.getStatus() != 1) { // 1表示已审核通过
                return false;
            }
            // 检查项目是否存在
            Project project = projectMapper.selectById(projectId);
            if (project == null) {
                return false;
            }
            // 检查是否已经参与该项目
            if (volunteerMapper.checkVolunteerProjectExists(volunteerId, projectId)) {
                return false;
            }
            // 实现志愿者参与项目的逻辑
            return volunteerMapper.insertVolunteerProject(volunteerId, projectId) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Page<Project> getVolunteerProjects(Long volunteerId, int pageNum, int pageSize) {
        try {
            Page<Project> page = new Page<>(pageNum, pageSize);
            return volunteerMapper.selectVolunteerProjects(volunteerId, page);
        } catch (Exception e) {
            return new Page<>();
        }
    }

    @Override
    @Transactional
    public boolean quitProject(Long volunteerId, Long projectId) {
        try {
            // 检查志愿者是否存在
            Volunteer volunteer = volunteerMapper.selectById(volunteerId);
            if (volunteer == null) {
                return false;
            }
            // 检查项目是否存在
            Project project = projectMapper.selectById(projectId);
            if (project == null) {
                return false;
            }
            // 检查是否参与了该项目
            if (!volunteerMapper.checkVolunteerProjectExists(volunteerId, projectId)) {
                return false;
            }
            // 实现退出项目的逻辑
            return volunteerMapper.deleteVolunteerProject(volunteerId, projectId) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateVolunteerStatus(Long volunteerId, Integer status) {
        try {
            Volunteer volunteer = volunteerMapper.selectById(volunteerId);
            if (volunteer == null) {
                return false;
            }
            volunteer.setStatus(status);
            return volunteerMapper.updateById(volunteer) > 0;
        } catch (Exception e) {
            return false;
        }
    }
}